import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

import { useMemberStore } from '@/stores/user'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/',
      name: 'houenum',
      component: () => import('../views/Houes/IndexView.vue'),
    },
    {
      path: '/Add',
      name: 'addhouenum',
      component: () => import('../views/Houes/AddView.vue'),
    },
    {
      path: '/Login',
      name: 'Login',
      component: () => import('../views/Member/LoginView.vue'),
    },
  ],
})



// BAD
router.beforeEach((to, from, next) => {
  //实现判断没有登录时，只能打开登录页面
  const member = useMemberStore(); //*****注意写在方法里 */
  if(member.userinfo.islogin != true &&  to.path!="/Login"){
    next({path:'/Login'})
  }
  else
  {
    next()
  }
})


export default router
